package liangtWorkSpace.validation.base;


import liangtWorkSpace.validation.base.ValidationBeanModel;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



import javax.validation.constraints.DecimalMax;
import java.util.List;

/**
 * Controller层 --- 初步简单测试 @Validated 的使用位置
 *
 * 对比测试过程:
 *    方案一 : 不在类上加@Validated注解，访问这六个接口
 *    方案二 : 在类上加@Validated注解，再次访问这六个接口
 *
 *    对比方案一和方案二，可初步得出@Validated的使用时机:
 *        1.当我们是在模型里面对模型字段添加约束注解，在Controller中使用模型接收数
 *          据时，@Validated要直接放在该模型参数前才有效。 如: "/test/one"
 *        2.当我们是直接在Controller层中的参数前，使用约束注解时，@Validated要直接放在类上，
 *          才会有效。如: /test/six
 *
 *
 * @author JustryDeng
 * @date 2019/1/18 22:22
 */
@RestController
@Validated
public class JustryDengController {
 
 
    @RequestMapping(value = "/test/one")
    public String validatioOne(@Validated ValidationBeanModel.AbcDecimalMax myDecimalMax) {
        System.out.println(myDecimalMax.getMyDecimalMax());
        return "one pass!";
    }
 
    @RequestMapping(value = "/test/two")
    @Validated
    public String validatioTwo(ValidationBeanModel.AbcDecimalMax myDecimalMax) {
        System.out.println(myDecimalMax.getMyDecimalMax());
        return "two pass!";
    }
 
    @RequestMapping(value = "/test/three")
    public String validatioThree(ValidationBeanModel.AbcDecimalMax myDecimalMax) {
        System.out.println(myDecimalMax.getMyDecimalMax());
        return "three pass!";
    }
 
    @RequestMapping(value = "/test/four")
    public String validatioFour(@Validated  @DecimalMax(value = "12.3") String myDecimalMax) {
        System.out.println(myDecimalMax);
        return "four pass!";
    }
 
    @RequestMapping(value = "/test/five")
    @Validated
    public String validatioFive(@DecimalMax(value = "12.3") String myDecimalMax) {
        System.out.println(myDecimalMax);
        return "five pass!";
    }
 
    @RequestMapping(value = "/test/six")
    @Validated
    public String validatioSix(@DecimalMax(value = "12.3") String myDecimalMax) {
        System.out.println(myDecimalMax);
        return "six pass!";
    }

    @RequestMapping(value = "/test/seven")
    public String validationSeven(@ConstraintsJustryDeng(contains = "") String string){
        System.out.println(string);
        return "seven pass!";
    }

    @RequestMapping(value = "/test/nine")
    public String validationNine(@Validated @RequestBody User user){
        System.out.println(user);
        return "seven pass!";
    }

    @RequestMapping
    public String validationeight(@Validated @RequestBody User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            List<ObjectError> errorList = bindingResult.getAllErrors();
            errorList.forEach(a->{
                System.out.println(a.getDefaultMessage());
            });
        }
        return "eight pass!";
    }
}
